import { TierCollectionEntity, TierDto } from "../../../models/tier.model";
import { UaIpEntity } from "../../../models/ua-ip.model";
import { UaUserDeckDto, UaUserDeckSearchDto } from "../../../models/ua-user-deck.model";
import { GlobalService } from "../../../services/global.service";
import { UaIpService } from "../../../services/ua-ip.service";
import { UaTierService } from "../../../services/ua-tier.service";
import { UserDeckService } from "../../../services/ua-user-deck.service";

// pages/lobby/tier/tier.page.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    FILE_ASSETS_HOST: GlobalService.STATIC_ASSETS_HOST,
    COLOR: [
      'rgb(255, 127, 127)',
      'rgb(255, 191, 127)',
      'rgb(255, 223, 127)',
      '#FFFF7F',
      'rgb(191, 255, 127)',
      'rgb(127, 255, 127)',
      'rgb(127, 255, 255)',
      'rgb(127, 191, 255)'
    ],
    STATIC_FILE_HOST: GlobalService.STATIC_FILE_HOST,
    FILE_HOST: GlobalService.FILE_HOST,
    TIER: '/marketing/tier.png',
    popupVisible: false,
    filtedList: [] as TierDto[],
    ipList: [] as { label: string, value: number }[],
  },


  customData: {
    searchRequest: {} as UaUserDeckSearchDto,
    tierList: [] as TierDto[],
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {

    const collection = options.collection;
    UaTierService.getTierCollectionById(collection).then((res: TierCollectionEntity) => {
      wx.setNavigationBarTitle({
        title: res.name!
      });
      this.setData({
        TIER: `/marketing/tier${collection}.png`,
        hint: res.desc!
      });
    })

    UaIpService.fetchUaIpList().then((response: UaIpEntity[]) => {
      const ipList: { label: string, value: number }[] = [];
      const ipLogoMap: { [key: number]: string } = {};
      for (const ip of response) {
        ipList.push({
          label: ip.ipTitleCn!,
          value: ip.id!
        });
        ipLogoMap[ip.id!] = ip.ipTitleLogo!;
      }
      this.setData({
        ipList,
        ipLogoMap,
      });
    });
    UaTierService.getTierDto(collection)
      .then((response: TierDto[]) => {
        for (const tier of response ?? []) {
          for (const deck of tier.decks ?? []) {
            deck.coverCardList = [];
            if (deck.cover) {
              const coverCardList = deck.cover.split(';');
              for (const url of coverCardList) {
                deck.coverCardList.push(url.replace('png', 'jpg'));
              }
            }
          }
        }
        this.customData.tierList = response;
        this.onStartFilter();
      })
  },

  view(e: any) {
    console.log(e.currentTarget.dataset)
    UserDeckService.findById(e.currentTarget.dataset['deckid']).then((response: UaUserDeckDto) => {
      wx.navigateTo({
        url: `/pages/user/user-deck-detail/user-deck-detail.page?ipId=${response.ipId}&deckId=${response.id}`,
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  filter() {
    this.setData({
      popupVisible: true
    })
  },

  onPopupVisibleChange(e: any) {
    this.setData({
      popupVisible: e.detail.visible
    });
  },

  onReset(e: any) {
    this.customData.searchRequest = e.detail;
    this.onStartFilter();
  },

  onSearch(e: any) {
    this.setData({
      popupVisible: false
    });
    this.customData.searchRequest = e.detail;
    this.onStartFilter();
  },

  onStartFilter() {
    const filtedList: TierDto[] = [];
    if (!this.customData.searchRequest.mainEnergyType && !this.customData.searchRequest.ipId) {
      this.setData({
        filtedList: JSON.parse(JSON.stringify(this.customData.tierList))
      });
      return;
    }
    for (const tier of this.customData.tierList) {
      const t = new TierDto();
      t.name = tier.name;
      t.decks = [];
      for (const deck of tier.decks ?? []) {
        let canadd = true;
        if (!!this.customData.searchRequest.ipId && deck.ipId !== this.customData.searchRequest.ipId) {
          canadd = false;
        }
        if (!!this.customData.searchRequest.mainEnergyType && deck.mainEnergyType !== this.customData.searchRequest.mainEnergyType) {
          canadd = false;
        }
        if (canadd) {
          t.decks.push(JSON.parse(JSON.stringify(deck)));
        }
      }
      filtedList.push(t);
    }
    this.setData({
      filtedList
    })
  }

})